package com.senseuni.da.ui.dialog

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.app.Dialog
import android.content.Context
import android.os.PowerManager
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.senseuni.da.R
import com.senseuni.da.listener.OnMyClickListener
import com.senseuni.da.utils.AppUtils

class PowerDialog2(val context: Context, private val listener: OnMyClickListener<String>?) {

    private var dialog: Dialog? = null

    init {
        val inflater = LayoutInflater.from(context)
        val layout = inflater.inflate(R.layout.dialog_power, null)
        initView(layout)

        val builder = AlertDialog.Builder(context)
        builder.setView(layout)
        builder.setCancelable(true)
        dialog = builder.create()
    }

    @SuppressLint("HardwareIds")
    private fun initView(layout: View) {
        layout.findViewById<TextView>(R.id.tv_power).setOnClickListener {
            AppUtils.shutdown()
        }

        layout.findViewById<TextView>(R.id.tv_reboot).setOnClickListener {
//            AppUtils.reboot()
            val powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager
            powerManager.reboot("")
        }

        layout.findViewById<TextView>(R.id.tv_sleep).setOnClickListener {
            AppUtils.screenOff()
        }
    }

    fun show() {
        dialog?.window?.let { window ->
            // 获取对话框的LayoutParams，以便我们可以修改其属性
            val layoutParams = window.attributes
            // 设置宽度为包裹内容，这样它将根据 R.layout.dialog_power 的定义进行调整
            layoutParams.width = 376
            // 如果你也想让高度跟随布局文件，可以这样做：
            // layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT
            // 应用修改后的参数
            window.attributes = layoutParams
        }
        dialog?.show()
    }

}